/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制
 */
package co.yixiang.controller.manager.shop;

import co.yixiang.common.annotation.Log;
import co.yixiang.common.utils.ResultUtil;
import co.yixiang.common.vo.ResultMessage;
import co.yixiang.modules.goods.entity.domain.YxStoreProductUnit;
import co.yixiang.modules.goods.entity.dto.YxStoreProductUnitQueryCriteria;
import co.yixiang.modules.goods.service.YxStoreProductUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author hupeng
 * @date 2019-10-04
 */
@Api(tags = "商城:商品计量单位接口")
@RestController
@RequestMapping("api/yxStoreProductUnit")
public class StoreProductUnitController {

    @Autowired
    private YxStoreProductUnitService productUnitService;

    @Log("分页获取商品计量单位")
    @ApiOperation(value = "分页获取商品计量单位")
    @GetMapping
    @PreAuthorize("hasAnyRole('admin','common','YXSTOREPRODUCTUNIT_ALL','YXSTOREPRODUCTUNIT_SELECT')")
    public ResponseEntity getYxStoreProducts(YxStoreProductUnitQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity(productUnitService.queryAll(criteria, pageable), HttpStatus.OK);
    }


    @Log("分页获取商品计量单位")
    @ApiOperation(value = "分页获取商品计量单位")
    @GetMapping(value = "/list")
    @PreAuthorize("hasAnyRole('admin','common','YXSTOREPRODUCTUNIT_ALL','YXSTOREPRODUCTUNIT_SELECT')")
    public ResponseEntity getYxStoreProductAll(YxStoreProductUnitQueryCriteria criteria ) {
        return new ResponseEntity(productUnitService.queryAll(criteria), HttpStatus.OK);
    }


    @ApiOperation(value = "获取商品计量单位")
    @ApiImplicitParam(name = "id", value = "计量单位ID", required = true, paramType = "path")
    @GetMapping("/{id}")
    public ResultMessage<YxStoreProductUnit> getById(@NotNull @PathVariable String id) {
        return ResultUtil.data(productUnitService.getById(id));
    }

    @ApiOperation(value = "添加商品计量单位")
    @PostMapping
    public ResultMessage<YxStoreProductUnit> save(@Valid @RequestBody YxStoreProductUnit goodsUnit) {
        productUnitService.save(goodsUnit);
        return ResultUtil.data(goodsUnit);
    }

    @ApiOperation(value = "编辑商品计量单位")
    @ApiImplicitParam(name = "id", value = "计量单位ID", required = true, paramType = "path")
    @PutMapping("/{id}")
    public ResultMessage<YxStoreProductUnit> update(@NotNull @PathVariable Long id, @Valid YxStoreProductUnit goodsUnit) {
        goodsUnit.setId(id);
        productUnitService.updateById(goodsUnit);
        return ResultUtil.data(goodsUnit);
    }

    @ApiOperation(value = "删除商品计量单位")
    @ApiImplicitParam(name = "ids", value = "计量单位ID", required = true, paramType = "path")
    @DeleteMapping("/{ids}")
    public ResultMessage<Object> delete(@NotNull @PathVariable List<Long> ids) {
        productUnitService.removeByIds(ids);
        return ResultUtil.success();
    }


}
